﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using LJTest.entity;
using System.Data.SQLite;
using System.Windows.Forms;

//---------------------------------------------------------------------------------//

//--------------------------------产生预取数据集算法-------------------------------//

//---------------------------------------------------------------------------------//

namespace LJTest
{
    class alwaysprefetch
    {
        ArrayList total = new ArrayList();
        ArrayList sub = new ArrayList();
        String InLine;

        public alwaysprefetch()
        {
            readRules1();
            //readFrequency();
        }
        /* **
         * 读取关联规则文件
         * 第一类关联规则，类似0.3|->15
         * 
         ** */
        private void readRules1()
        {
            if (File.Exists(Common.RULE_FILE_PATH))
            {
                try
                {


                    //从文件中读取数据并保存到数组Lc中,项目已经分割并保存在ArrayList数组Lc中，Lc的每个子元素也是ArrayList数组，0元素为事务的编号，1~N元素为项集
                    using (StreamReader sr = new StreamReader(Common.RULE_FILE_PATH, Encoding.GetEncoding("gb2312")))
                    {
                        while ((InLine = sr.ReadLine()) != null)
                        {
                           
                            int firstIndex = InLine.IndexOf("|->");
                            if (firstIndex > 0)
                            {
                                double mf = 0;
                                mf = Convert.ToDouble(InLine.Substring(0, firstIndex));
                                sub.Add(InLine.Substring(firstIndex+3)); //确定只有一个数字，没有逗号
                                total.Add(new Rule(mf, sub));
                                sub = new ArrayList();

                            }
                           
                        }
                    }

                  //  rules = total;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("读取关联规则中的记录出错：" + ex.Message);

                }
            }

        }



        //获得总是预取数据集合，并放入cache中
        public static void alwayspres()
        {
            alwaysprefetch ap = new alwaysprefetch();
            StreamWriter transtreamwriter = new StreamWriter(Common.CACHE_FILE, true);
            CaculateDataValue(ap.total);
            foreach (ArrayList a in ap.total)
            {
                
                transtreamwriter.WriteLine(a[0]);
            }

            transtreamwriter.Close();

        }


        //获得预取数据价值，并进行排序
        public static void CaculateDataValue(ArrayList datalist)
        {
            //Hashtable hashtable = new Hashtable();
            ArrayList ValueList = new ArrayList();
            foreach (Rule rule in datalist)
            {
                ArrayList data = rule.rule;
                string datastr = (string)data[0];
                //ValueList.Add(getDataValue(datastr));
                ValueList.Add(new Datalog(datastr, getDataValue(datastr)));
            }
            ValueList.Sort();
            

        }

        public static double getDataValue(string data)
        {
            string sql = "SELECT datasize,strftime('%s',updatatime2) - strftime('%s',updatatime1) as time from datalog where 'data'=" + data; ;
            double value = 0;
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader(sql);
                double datasize = 0;
                double time = 0;
                while (reader.Read())
                {
                     datasize = Convert.ToDouble(reader.GetString(0));
                     time = Convert.ToDouble(reader.GetString(1));
                }
                reader.Close();
                if (time == 0)
                {
                    value = 0;
                }
                else
                {
                    value = (1 - 2 / time) / datasize;
                }
                return value;
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0.0;
            }

        }

    }
}